package hichecker.ui.custom_views.requirement;

import hichecker.controller.Config;
import hichecker.controller.MainController;
import hichecker.tools.export.CSVFile;
import hichecker.ui.components.custom_table.CustomTable;
import hichecker.ui.components.icon_factory.IconFactory;
import hichecker.ui.general_views.container_panel.content.ContentPanel;
import hichecker.university.Requirement;
import hichecker.university.UniversityPart;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle;

public class RequirementView extends ContentPanel {
	private MainController controller;
	private Requirement requirement;
	private RequirementViewTableModel model;
	private CustomTable tblRequirement;

	public RequirementView(UniversityPart item, MainController controller) {
		super(item);
		this.controller = controller;
		this.requirement = (Requirement) item;

		model = new RequirementViewTableModel(requirement, controller.getStudents());
		tblRequirement = new CustomTable(model, getWidth());
		JScrollPane pane = new JScrollPane(tblRequirement);

		AbstractAction deleteAction = new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				deleteLines();
			}
		};
		JButton btnDelete = IconFactory.getIconButton(Config.BUTTON_REMOVE, null, null, deleteAction);
		tblRequirement.getInputMap().put(Config.HOTKEY_DELETE, "delete");
		tblRequirement.getActionMap().put("delete", deleteAction);

		// Layout
		GroupLayout layout = new GroupLayout(this);
		setLayout(layout);

		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		layout.setHorizontalGroup(layout.createSequentialGroup().addGroup(
				layout.createParallelGroup().addComponent(pane).addGroup(
						layout.createSequentialGroup().addPreferredGap(
								LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE).addComponent(btnDelete))));

		layout.setVerticalGroup(layout.createSequentialGroup().addComponent(pane).addGroup(
				layout.createParallelGroup().addComponent(btnDelete)));
	}

	private void deleteLines() {
		// No line selected
		if (tblRequirement.getSelectedRow() < 0) return;
		if (!userConfirmed()) return;

		int[] selectedRows = tblRequirement.getSelectedModelRows();
		for (int i = selectedRows.length - 1; i >= 0; i--)
			model.deleteRow(selectedRows[i]);
	}

	@Override
	public boolean isCorrect() {
		boolean isCorrect = tblRequirement.isCorrect();
		if (isCorrect) tblRequirement.setCurrentValue();
		return isCorrect;
	}

	@Override
	public void paint(Graphics g) {
		tblRequirement.setAutoResizeable(getWidth());
		super.paint(g);
	}

	@Override
	public ContentPanel switchViewSettings() {
		return new RequirementSettings(item, controller);
	}

	@Override
	public List<CSVFile> toCSV(String topLevel) {
		String name = topLevel + " - " + item.getName();

		List<CSVFile> files = new ArrayList<CSVFile>();
		files.add(model.toCSV(name));

		for (UniversityPart part : item.getSubParts())
			files.addAll(part.getRepresentation(false, controller).toCSV(name));

		return files;
	}
}
